// ManipulatorGLObject.cpp

# include "ManipulatorGLObject.h"

namespace Voltron
{

	ManipulatorGLObject::ManipulatorGLObject(): GLObject3D()
	{
				_visible=false;
				flag=2;
				
				CreateAxesGeometry();
				initColorBuffers();
				
	}

	ManipulatorGLObject::ManipulatorGLObject(GLuint shaderprogram): GLObject3D()
	{
				_visible=false;
				flag=2;
				
				CreateAxesGeometry();
				initColorBuffers();
				attachVertexColorShader(shaderprogram);
				
	}
/*
	ManipulatorGLObject::ManipulatorGLObject(int quantityVerts):
				GLObject3D(quantityVerts)
	{
				_visible=false;
				flag=2;
	}

	ManipulatorGLObject::ManipulatorGLObject(int quantityVerts, vec4* positionVerts, vec4* colorVerts, GLuint shaderprogram):
	 			GLObject3D(quantityVerts,positionVerts, colorVerts, shaderprogram)
	{
                _model = identity();
				_visible=false;
				flag=2;
				CreateAxesGeometry();
				initColorBuffers();
	}
*/



	ManipulatorGLObject::~ManipulatorGLObject()
	{
	}

	void ManipulatorGLObject::CreateAxesGeometry()
	{
		// Create normalized line points for each axis with appropriate color
		// X-axis - solid red
		_positionVerts.push_back(point4(0.0,0.0,0.0,1.0));
		_colorVerts.push_back(color4(1.0,0.0,0.0,1.0));

		_positionVerts.push_back(point4(1.0,0.0,0.0,1.0));
		_colorVerts.push_back(color4(1.0,0.0,0.0,1.0));

		_positionVerts.push_back(point4(1.0,0.1,0.0,1.0));
		_colorVerts.push_back(color4(1.0,0.0,0.0,1.0));

		_positionVerts.push_back(point4(0.8,0.0,0.0,1.0));
		_colorVerts.push_back(color4(1.0,0.0,0.0,1.0));

 
		// Y-axis - solid green
		_positionVerts.push_back(point4(0.0,0.0,0.0,1.0));
		_colorVerts.push_back(color4(0.0,1.0,0.0,1.0));

		_positionVerts.push_back(point4(0.0,1.0,0.0,1.0));
		_colorVerts.push_back(color4(0.0,1.0,0.0,1.0));

		_positionVerts.push_back(point4(0.0,1.0,0.0,1.0));
		_colorVerts.push_back(color4(0.1,1.0,0.0,1.0));

		_positionVerts.push_back(point4(0.0,1.0,0.0,1.0));
		_colorVerts.push_back(color4(0.0,0.8,0.0,1.0));
		 
		// Z-axis - solid blue
		_positionVerts.push_back(point4(0.0,0.0,0.0,1.0));
		_colorVerts.push_back(color4(0.0,0.0,1.0,1.0));

		_positionVerts.push_back(point4(0.0,0.0,1.0,1.0));
		_colorVerts.push_back(color4(0.0,0.0,1.0,1.0));

		_positionVerts.push_back(point4(0.0,0.0,1.0,1.0));
		_colorVerts.push_back(color4(0.1,0.0,1.0,1.0));

		_positionVerts.push_back(point4(0.0,0.0,1.0,1.0));
		_colorVerts.push_back(color4(0.0,0.0,0.8,1.0));
		
	}


	void ManipulatorGLObject::render()
	{
		if(_visible)
		{
			makeActiveObject();
			glUniform1i(SelectFlagLoc, 0);
			
			// draw X manipulator axis
			glDrawArrays(GL_LINES, 0, 4);

			// draw Y manipulator axis
			glDrawArrays(GL_LINES, 4, 4);

			// draw Z manipulator axis
			glDrawArrays(GL_LINES, 8, 4);

		}
		
	}

	void ManipulatorGLObject::initColorBuffers()
	{
		makeActiveObject();
		glGenBuffers(1, &_vbo[GLObject3D::COLOR]);

		glBindBuffer(GL_ARRAY_BUFFER, _vbo[GLObject3D::COLOR]);
		glBufferData(GL_ARRAY_BUFFER, _colorVerts.size()*sizeof(vec4), &_colorVerts[0], GL_STATIC_DRAW);
	}

	bool ManipulatorGLObject::isSelected(GLubyte pixel[4])
	{
		if(_selectionColor[0] == ceil(pixel[0]) && _selectionColor[1] == pixel[1]
				&& _selectionColor[2] == pixel[2] && _selectionColor[3] == pixel[3])
			return true;
		return false;
	}

	mat4 ManipulatorGLObject::transformModel(mat4 transform)
	{
		_model = _model * transform;
		return _model;
	}
	
}

